|
|
|
|
|
|
|
The primary advantage of the Error object is that it can report additional error information not included in the standard Err object mentioned earlier in this chapter. Many times, your database application needs to depend on external processes such as ODBC data connections or OLE Server modules. When an error occurs in these external processes, they may report more than one error code back to your Visual Basic application. |
|
|
|
|
|
|
|
|
The Err object is only able to remember the most recent error reported. However, the Error object (and its associated Errors collection) can remember all of the errors reported by external processes. That is why it is a good idea to use the Error object for reporting errors in all your Visual Basic database programs. |
|
|
|
|
|
|
|
|
The properties of the Microsoft data access Error object are almost identical to the properties of the Visual Basic Err object (see the discussion earlier in this chapter). The only difference is that the Error object does not have the optional LastDLLError property. Therefore, the calling convention for the Error object is basically the same as that for the Err object: |
|
|
|
|
|
|
|
|
Msgbox "<" & CStr(Error.Number) & "> " & Error.Description & "[" & Error.Source
& "]" |
|
|
|
|
|
|
|
|
While the Error and Err objects are quite similar, there is one major difference worth noting. While the Err object stands alone, the Microsoft data access Error object belongs to the Errors collection. This is very important when dealing with back-end database servers, especially when your Visual Basic program is connected to databases through the Open Database Connectivity (ODBC) interface. When an error occurs during an ODBC transaction, the Err object always returns the same error message, ODBC failed. However, the Errors collection often contains more than one error message, which can tell you a great deal more about the nature of the problem. You can retrieve all the error information by enumerating all the Error objects in the Errors collection. The code in Listing 14.1 shows how that can be done. |
|
|
|
|
|
|
|
|
Listing 14.1. Enumerating the Errors collection. |
|
|
|
|
|
|
|
|
Dim objTempErr as Object
Dim strMsg as String
For Each objTempErr In Errors
StrMsg = "<" & CStr(objTempErr.Number) & "> "
StrMsg = strMsg & objTempErr.Description
StrMsg = strMsg & " in [" & objTempErr.Source & "]" & vbCrLf
Next
Msgbox strMsg |
|
|
|
|
|
|
|
|
The code in Listing 14.1 creates a single line of text (strMsg) that contains all the error messages reported by the back-end database server. You learn more about using both the Err and Error objects in the next section of the chapter. |
|
|
|
|
|